# -*- coding:utf-8 -*-

import os
import yaml
from munch import munchify, Munch


class BaseConfig(object):
	# 项目根路径
	rootPath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

	# 获取配置Url地址
	@classmethod
	def getUrlDict(cls) -> Munch:
		urlDictPath = os.path.join(BaseConfig.rootPath, "Config", 'test', 'UrlConfig.yml')
		with open(urlDictPath, 'rb') as f:
			urlDict = yaml.load(f, Loader=yaml.FullLoader)
		host = urlDict.pop("host")
		urlDict["url"] = {k: host + v for k, v in urlDict.get("url").items()}
		urlDict = munchify(urlDict)
		return urlDict

	# 获取全局配置
	@classmethod
	def getGlobalConf(cls, key=None) -> Munch:
		globalConfPath = os.path.join(BaseConfig.rootPath, "Config", 'test', 'globalConf.yml')
		with open(globalConfPath, 'rb') as f:
			globalConf = yaml.load(f, Loader=yaml.FullLoader)
		if key:
			globalConf = globalConf.get(key)
		globalConf = munchify(globalConf)
		return globalConf

	# 获取接口平台配置
	@classmethod
	def getInterfaceConf(cls) -> Munch:
		interfaceConfPath = os.path.join(BaseConfig.rootPath, "Config", 'test', 'interface.yml')
		with open(interfaceConfPath, 'rb') as f:
			interfaceConf = yaml.load(f, Loader=yaml.FullLoader)
		interfaceConf = munchify(interfaceConf)
		return interfaceConf


class Config(BaseConfig):

	# 配置文件路径
	@property
	def configPath(self):
		return os.path.join(BaseConfig.rootPath, "Config")

	# 测试数据配置路径
	@property
	def testDataPath(self):
		return os.path.join(BaseConfig.rootPath, "Data")

	# 图片保存路径
	@property
	def imagePath(self):
		return os.path.join(BaseConfig.rootPath, "Images")

	# 日志保存路径
	@property
	def logPath(self):
		return os.path.join(BaseConfig.rootPath, "Log")

	# url配置文件数据
	@property
	def urlDict(self) -> Munch:
		return self.getUrlDict()

	# 全局配置
	@property
	def globalConf(self) -> Munch:
		return self.getGlobalConf()

	# 公共配置
	@property
	def globalPublicConf(self) -> Munch:
		return self.getGlobalConf(key="publicConf")

	# 浏览器配置
	@property
	def globalBrowserConf(self) -> Munch:
		return self.getGlobalConf(key="browserConf")

	# 日志配置
	@property
	def globalLogConf(self) -> Munch:
		return self.getGlobalConf(key="logConf")

	# 接口平台配置
	@property
	def interfaceConf(self) -> Munch:
		return self.getInterfaceConf()

	@property
	def fscNodes(self) -> Munch:
		return self.urlDict.fscNodes

	@property
	def businessParams(self) -> Munch:
		return self.urlDict.businessParams

	@property
	def basMenuConfPath(self):
		return os.path.join(self.configPath, 'test', 'basMenuConf.yml')

	@property
	def basMenuConf(self) -> Munch:
		with open(self.basMenuConfPath, "r", encoding="utf-8") as f:
			data = yaml.full_load(f)
		return munchify(data)


config = Config()

if __name__ == '__main__':
	r = config.basMenuConfPath
	print(r)
